/**
 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "gtest/gtest.h"
#include "test_helper.h"

#include "plugins/ecmascript/runtime/builtins.h"
#include "plugins/ecmascript/runtime/ecma_vm.h"
#include "plugins/ecmascript/runtime/global_env.h"
#include "plugins/ecmascript/runtime/js_thread.h"
#include "include/runtime.h"
#include "plugins/ecmascript/runtime/js_handle.h"
#include "plugins/ecmascript/runtime/js_object.h"
#include "plugins/ecmascript/runtime/base/builtins_base.h"

using ark::ecmascript::GlobalEnv;
using ark::ecmascript::JSFunction;
using ark::ecmascript::JSHandle;
using ark::ecmascript::JSObject;
using ark::ecmascript::ObjectFactory;

namespace ark::test {
class BuiltinsTest : public testing::Test {
public:
    void SetUp() override
    {
        TestHelper::CreateEcmaVMWithScope(instance_, thread_, scope_);
    }

    void TearDown() override
    {
        TestHelper::DestroyEcmaVMWithScope(instance_, scope_);
    }

protected:
    // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes)
    JSThread *thread_ {};

private:
    PandaVM *instance_ {nullptr};
    EcmaHandleScope *scope_ {nullptr};
};

TEST_F(BuiltinsTest, ObjectInit)
{
    ASSERT_NE(thread_, nullptr);
    auto ecmaVm = thread_->GetEcmaVM();
    JSHandle<GlobalEnv> env = ecmaVm->GetGlobalEnv();

    JSHandle<JSFunction> objectFunction(env->GetObjectFunction());
    ASSERT_NE(*objectFunction, nullptr);
}

TEST_F(BuiltinsTest, FunctionInit)
{
    ASSERT_NE(thread_, nullptr);
    auto ecmaVm = thread_->GetEcmaVM();
    JSHandle<GlobalEnv> env = ecmaVm->GetGlobalEnv();

    JSHandle<JSFunction> functionFunction(env->GetFunctionFunction());
    ASSERT_NE(*functionFunction, nullptr);
}

TEST_F(BuiltinsTest, NumberInit)
{
    ASSERT_NE(thread_, nullptr);
    auto ecmaVm = thread_->GetEcmaVM();
    JSHandle<GlobalEnv> env = ecmaVm->GetGlobalEnv();

    JSHandle<JSFunction> numberFunction(env->GetNumberFunction());
    ASSERT_NE(*numberFunction, nullptr);
}

TEST_F(BuiltinsTest, SetInit)
{
    ASSERT_NE(thread_, nullptr);
    auto ecmaVm = thread_->GetEcmaVM();
    JSHandle<GlobalEnv> env = ecmaVm->GetGlobalEnv();

    JSHandle<JSFunction> setFunction(env->GetSetFunction());
    ASSERT_NE(*setFunction, nullptr);
}

TEST_F(BuiltinsTest, MapInit)
{
    ASSERT_NE(thread_, nullptr);
    auto ecmaVm = thread_->GetEcmaVM();
    JSHandle<GlobalEnv> env = ecmaVm->GetGlobalEnv();

    JSHandle<JSFunction> mapFunction(env->GetMapFunction());
    ASSERT_NE(*mapFunction, nullptr);
}

TEST_F(BuiltinsTest, StrictModeForbiddenAccess)
{
    ASSERT_NE(thread_, nullptr);
    auto ecmaVm = thread_->GetEcmaVM();
    JSHandle<GlobalEnv> env = ecmaVm->GetGlobalEnv();
    ObjectFactory *factory = thread_->GetEcmaVM()->GetFactory();

    JSHandle<JSFunction> function = factory->NewJSFunction(env, static_cast<void *>(nullptr));

    JSHandle<JSTaggedValue> callerKey(factory->NewFromString("caller"));
    JSHandle<JSTaggedValue> argumentsKey(factory->NewFromString("arguments"));

    JSObject::GetProperty(thread_, JSHandle<JSTaggedValue>(function), callerKey);
    ASSERT_EQ(thread_->HasPendingException(), true);

    JSObject::GetProperty(thread_, JSHandle<JSTaggedValue>(function), argumentsKey);
    ASSERT_EQ(thread_->HasPendingException(), true);
}

}  // namespace ark::test
